/*
 * @(#) LTFormatUtil.java  1.0  2004-4-7
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.bas.util;

import java.sql.Timestamp;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;


public class LTFormatUtil {

	public static String DATE_FORMAT_MM_DD = "yyyy-MM-dd HH:mm";

	// 存放颜色配置信息,存于缓存
	public static HashMap mapCache = new HashMap();

	public static String replace(String strSource, String strFrom, String strTo) {
		String strDest = "";
		int intFromLen = strFrom.length();
		int intPos;

		while ((intPos = strSource.indexOf(strFrom)) != -1) {
			strDest = strDest + strSource.substring(0, intPos);
			strDest = strDest + strTo;
			strSource = strSource.substring(intPos + intFromLen);
		}
		strDest = strDest + strSource;

		return strDest;
	}

	// ��4�滻����html��tag,��4����ͨ�û�ʹ�ã�����ڿ�
	public static String formatStrAll(String strSrc, int lngStrLen) {
		String strRst = strSrc;
		// �滻���Ϸ��ַ�
		strRst = replace(strRst, "<", "&lt;");
		strRst = replace(strRst, ">", "&gt;");
		strRst = replace(strRst, " ", "&nbsp;");
		strRst = replace(strRst, "\n", "<br>");
		strRst = replace(strRst, "\r", "");
		// ��ȡ����Ҫ�ĳ���
		if (lngStrLen > 0) {
			if (lngStrLen > strRst.length()) {
				lngStrLen = strRst.length();
			}
			strRst = strRst.substring(0, lngStrLen);
		}
		return strRst;
	}

	// ��4�滻����html��tag����4��ȷ����ʾ���
	public static String formatStr(String strSrc, int lngStrLen) {
		String strRst = strSrc;
		// �滻���Ϸ��ַ�
		strRst = replace(strRst, "<", "&lt;");
		strRst = replace(strRst, ">", "&gt;");
		// strRst=replace(strRst," ","&nbsp;");
		// strRst=replace(strRst,"\n","<br>");
		// strRst=replace(strRst,"\r","");

		// ��ȡ����Ҫ�ĳ���
		if (lngStrLen > 0) {
			if (lngStrLen > strRst.length()) {
				lngStrLen = strRst.length();
			}
			strRst = strRst.substring(0, lngStrLen);
		}

		return strRst;
	}

	public static String subStr(String strSrc, int lngStrLen) {
		String strRst = strSrc;
		// ��ȡ����Ҫ�ĳ���
		if (lngStrLen > 0) {
			if (lngStrLen < strRst.length()) {
				if (lngStrLen > 3) {
					strRst = strRst.substring(0, lngStrLen - 3) + "...";
				} else {
					strRst = strRst.substring(0, lngStrLen);
				}
			}
		}
		return strRst;
	}

	public static String normalizeHTMLStr(String s) {
		String strResult = "";
		if ((s == null) || (s.equals("null")))
			return "";
		int intLen = s.trim().length();
		for (int i = 0; i < intLen; i++) {
			char c = s.charAt(i);
			strResult += (c == '"' ? "&#0034;" : (c == '\'' ? "&#0039;"
					: (c == '\n' ? "<br>" : (c == '\r' ? "&#0010;"
							: (c == '<' ? "&#0060;" : (c == '>' ? "&#0062;"
									: (c == '&' ? "&#0038;"
											: (c == ' ' ? "&nbsp;" : String
													.valueOf(c)))))))));
		}
		return strResult;
	}

	public static String quoteHTMLStr(String s) {
		String strResult = "";
		if ((s == null) || (s.equalsIgnoreCase("null")))
			return "\"\"";
		int intLen = s.trim().length();
		for (int i = 0; i < intLen; i++) {
			char c = s.charAt(i);
			strResult += (c == '"' ? "&#0034;" : (c == '\'' ? "&#0039;"
					: (c == '\n' ? "&#0013;" : (c == '\r' ? "&#0010;" : String
							.valueOf(c)))));
		}
		return "\"" + strResult + "\"";
	}

	public static String noQuoteHTMLStr(String s) {
		String strResult = "";
		if ((s == null) || (s.equalsIgnoreCase("null")))
			return "";
		int intLen = s.trim().length();
		for (int i = 0; i < intLen; i++) {
			char c = s.charAt(i);
			strResult += (c == '"' ? "&#0034;" : (c == '\'' ? "&#0039;"
					: (c == '\n' ? "&#0013;" : (c == '\r' ? "&#0010;" : String
							.valueOf(c)))));
		}
		return strResult;
	}

	public static String quoteJSStr(String s) {
		String strResult = "";
		if (s == null)
			return "\"\"";
		int intLen = s.length();
		for (int i = 0; i < intLen; i++) {
			char c = s.charAt(i);
			strResult += (c == '\\' ? "\\\\" : (c == '"' ? "\\\""
					: (c == '\n' ? "\\n" : (c == '\r' ? "\\r" : String
							.valueOf(c)))));
		}
		return "\"" + strResult + "\"";
	}

	public static String checked(String theoption, String theval) {
		String strChecked;
		if ((theoption == null) || (theval == null))
			strChecked = "";
		else if (theoption.trim().equalsIgnoreCase(theval.trim()))
			strChecked = "checked";
		else
			strChecked = "";
		return strChecked;
	}

	public static String radio(String theoption, String theval) {
		String strRadio;
		if ((theoption == null) || (theval == null))
			strRadio = "";
		else if (theoption.trim().equalsIgnoreCase(theval.trim()))
			strRadio = "checked";
		else
			strRadio = "";
		return strRadio;
	}

	public static String selected(String theoption, String theval) {
		String strselected;
		if ((theoption == null) || (theval == null))
			strselected = "";
		else if (theoption.trim().equalsIgnoreCase(theval.trim()))
			strselected = "selected";
		else
			strselected = "";
		return strselected;
	}

	public static String formatDate(Date date, String pattern) {
		if (date == null)
			return "";
		if (pattern == null)
			pattern = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return (sdf.format(date));
	}

	public static String formatDateTime(Date date) {
		return (formatDate(date, "yyyy-MM-dd HH:mm:ss"));
	}

	public static String formatDateTime() {
		return (formatDate(now(), "yyyy-MM-dd HH:mm:ss"));
	}

	public static String formatDate(Date date) {
		return (formatDate(date, "yyyy-MM-dd"));
	}

	public static String formatDate() {
		return (formatDate(now(), "yyyy-MM-dd"));
	}

	public static String formatTime(Date date) {
		return (formatDate(date, "HH:mm:ss"));
	}

	public static String formatTime() {
		return (formatDate(now(), "HH:mm:ss"));
	}

	public static String formatTime2() {
		return (formatDate(now(), "HHmmss"));
	}

	public static Date now() {
		return (new Date());
	}

	public static String formatNumber(double decimal, int fraction) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(fraction);
		nf.setMinimumFractionDigits(fraction);
		return (nf.format(decimal));
	}

	public static String formatNumber(double decimal) {
		return formatNumber(decimal, 0);
	}

	public static Date parseDatetime(String datetime) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pp = new ParsePosition(0);
		if ((datetime == null) || (datetime.equals(""))) {
			return null;
		} else {
			try {
				return formatter.parse(datetime);
			} catch (ParseException e) {
				return parseDate(datetime);
			}
		}
	}

	public static Date parseDate(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

		if ((date == null) || (date.equals(""))) {
			return null;
		} else {
			try {
				return formatter.parse(date);
			} catch (ParseException e) {
				return null;
			}
		}
	}

	public static Date parseDate(String date, String pattern) {
		String format = "yyyy-MM-dd";
		if (!StringUtil.isEmpty(pattern))
			format = pattern;
		SimpleDateFormat formatter = new SimpleDateFormat(format);

		if ((date == null) || (date.equals(""))) {
			return null;
		} else {
			try {
				return formatter.parse(date);
			} catch (ParseException e) {
				return null;
			}
		}
	}

	public static Date parseDate(Date datetime) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

		if (datetime == null) {
			return null;
		} else {
			try {
				return formatter.parse(formatter.format(datetime));
			} catch (ParseException e) {
				return null;
			}
		}
	}

	public static String formatDate(Object o) {
		if (o == null)
			return "";
		if (o.getClass() == String.class)
			return formatDate((String) o);
		else if (o.getClass() == Date.class)
			return formatDate((Date) o);
		else if (o.getClass() == Timestamp.class) {
			return formatDate(new Date(((Timestamp) o).getTime()));
		} else
			return o.toString();
	}

	public static String formatDateTime(Object o) {
		if (StringUtil.null2Empty(String.valueOf(o)).equals(""))
			return "";
		if (o.getClass() == String.class)
			return formatDateTime((String) o);
		else if (o.getClass() == Date.class)
			return formatDateTime((Date) o);
		else if (o.getClass() == Timestamp.class) {
			return formatDateTime(new Date(((Timestamp) o).getTime()));
		} else
			return o.toString();
	}

	public static Date getDate() {
		return parseDate(formatDate());
	}

	public static Date addOneDay(Date date) {
		return addDays(date, 1);

	}

	public static Date addDays(Date date, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, days);
		return calendar.getTime();

	}

	public static String getFailureTime(Date beginDate, Date endDate) {
		if (StringUtil.isEmpty(beginDate) || StringUtil.isEmpty(endDate))
			return "";
		String str = "";
		long ms = endDate.getTime() - beginDate.getTime();
		String d = ms / (24 * 60 * 60 * 1000) + "";
		ms = ms % (24 * 60 * 60 * 1000);
		String h = ms / (60 * 60 * 1000) + "";
		ms = ms % (60 * 60 * 1000);
		String m = ms / (60 * 1000) + "";
		ms = ms % (60 * 1000);
		String s = ms / (1000) + "";
		str = d + "天" + h + "时" + m + "分" + s + "秒";
		return str;
	}

	public static String getHigh(String alarmType) {
		int high = 0;
		if (alarmType.equals(Constants.NORMAL_ALARM_A)) {
			high = Constants.NORAML_PRIORITY_HIGH;
		} else if (alarmType.equals(Constants.NORMAL_ALARM_B)) {
			high = Constants.NORAML_PRIORITY_HIGH;
		} else if (alarmType.equals(Constants.APOLLO_ALARM)) {
			high = Constants.APOLLO_PRIORITY_HIGH;
		} else if (alarmType.equals(Constants.RESOURCE_ALARM)) {
			high = Constants.RESOURCE_PRIORITY_HIGH;
		} else if (alarmType.equals(Constants.YW_ALARM)) {
			high = Constants.YW_PRIORITY_HIGH;
		} else if (alarmType.equals(Constants.TEST_ALARM)) {
			high = Constants.TEST_PRIORITY_HIGH;
		}

		return String.valueOf(high);
	}

	


}
